import { getCarList, startAssistCar } from '@/api/list'
import { useCarStore } from '@/stores/car'
import { ElMessage } from '@/stores/elMessage'
import { i18n } from '@/utils'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useChangeCar = defineStore('ChangeCar', () => {
  const carStore = useCarStore()
  const carDialogVisible = ref<boolean>(false)
  const assistCar = ref<string>('')

  const changeCar = (carCode: string) => {
    assistCar.value = carCode
  }

  const visibleChange = async (visible: boolean) => {
    if (visible) {
      const { data } = await getCarList('patroling')
      carStore.setCarList(data || [])
    }
  }
  const closeCarDialog = () => {
    carDialogVisible.value = false
  }

  const confirmIt = async () => {
    if (assistCar.value) {
      await startAssistCar({
        vehicleA: carStore.currentCar,
        vehicleB: assistCar.value
      })
      carDialogVisible.value = false
    } else {
      ElMessage({ type: 'error', message: i18n.global.t('qing-xuan-ze-che-liang') })
    }
  }
  return {
    changeCar,
    visibleChange,
    closeCarDialog,
    carDialogVisible,
    confirmIt,
    assistCar
  }
})
